using System;
using System.IO;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base;

namespace FireStarter.CodeGeneration
{
    public class UIWinProjectGenerator : ProjectGenerator
    {
        public override Result Generate()
        {
            CheckRequiredPropertiesSet();
            //if (ModelSolution.LogicProjectName == null) throw new PropertyMissingException("LogicProjectName");
            if (ModelSolution.BOProjectName == null) throw new PropertyMissingException("BoProjectName");
            if (SolutionGenerator.SolutionTypeName == null) throw new PropertyMissingException("SolutionTypeName");
            Directory.CreateDirectory(ProjectPath);

            TemplateLoader vwgProjectTemplateLoader = new TemplateLoader();

            string contents = vwgProjectTemplateLoader.GetAssemblyInfo(ProjectName);
            Utilities.CreateFile(ProjectPath, "Properties\\AssemblyInfo.cs", contents);

            string projectFileName = Path.Combine(ProjectPath, ProjectName + ".csproj");
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName, OutputType.Library, SolutionGenerator.CodeTemplate);

            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.BO.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Base.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "Habanero.Faces.Win.dll"));
            projectFileManager.AddReference(Path.Combine(SolutionGenerator.HabaneroReferencePath, "log4net.dll"));
            projectFileManager.AddSystemReference("System.Windows.Forms");

            projectFileManager.AddProjectReference(ModelSolution.UIProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.BOProjectName, SolutionGenerator.SolutionDirectory);
            projectFileManager.AddProjectReference(ModelSolution.LogicProjectName, SolutionGenerator.SolutionDirectory);

            if (GenerateControlFactory()) AddDefaultControlFactory(projectFileManager);

            projectFileManager.SaveProjectFile();
            return new Result(true);
        }

        protected bool GenerateControlFactory()
        {
            return SolutionGenerator.SolutionTypeName == GenerationTypes.SOLUTION_TYPE_DUALWINVWG;
        }

        private void AddDefaultControlFactory(ProjectFileManager projectFileManager)
        {
           // GenerationUtilities.GenerateCodeFile(projectFileManager, "Templates/UI_Win_ControlFactory.txt",
               // ProjectPath, SolutionGenerator.ModelSolution.ApplicationName + "ControlFactoryWin.cs", CreateProjectModelUIWin());

            UIWinProjectTemplateLoader uivwgProjectTemplateLoader = new UIWinProjectTemplateLoader();
            string contents = uivwgProjectTemplateLoader.GetControlFactory(ProjectName, SolutionGenerator.ModelSolution);
            string brokerName = Utilities.CreateFile(ProjectPath, SolutionGenerator.ModelSolution.ApplicationName + "ControlFactoryWin.cs", contents);
            projectFileManager.AddCodeFile(brokerName);
        }

        private ProjectModelUIWin CreateProjectModelUIWin()
        {
            return new ProjectModelUIWin
            {
                ProjectName = SolutionGenerator.ModelSolution.ApplicationName,
                UIBaseProjectName = GenerationUtilities.GetProjectName(ModelSolution.UIProjectName),
                UIWinProjectName = ProjectName
            };
        }

        private class ProjectModelUIWin : IModelElement
        {
            public string ProjectName { get; set; }
            public string UIWinProjectName { get; set; }
            public string UIBaseProjectName { get; set; }

            #region Implementation of IModelElement

            public IModelElement Parent
            {
                get { return null; }
                set { throw new System.NotImplementedException(); }
            }

            #endregion
        }

    }

}